package com.cdd.mvc;

import org.quartz.JobKey;
import org.quartz.Matcher;
import org.quartz.SchedulerException;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@RestController
public class JobController {

    @Autowired
    private JobUtil jobUtil;

    @Autowired
    private AppQuartzService appQuartzService;


    //添加一个job
    @RequestMapping(value="/addJob", method= RequestMethod.POST)
    public ReturnMsg addjob(@RequestBody AppQuartz appQuartz) throws Exception {
        appQuartzService.insertJob(appQuartz);
        String result = jobUtil.addJob(appQuartz);
        return new ReturnMsg("200", result);
    }

    //暂停job
    @RequestMapping(value="/pauseJob", method=RequestMethod.GET)
    public ReturnMsg pausejob(@RequestParam Integer quartzId) throws Exception {
        AppQuartz appQuartz = appQuartzService.queryOne(quartzId);
        Assert.notNull(appQuartz, "fail pauseJob");
        String rs = jobUtil.pauseJob(appQuartz.getJobName(), appQuartz.getJobGroup());
        return new ReturnMsg("200","success pauseJob");
    }

    // 恢复job
    @RequestMapping(value="/resumeJob", method=RequestMethod.GET)
    public ReturnMsg resumejob(@RequestParam Integer quartzId) throws Exception {
        AppQuartz appQuartz = appQuartzService.queryOne(quartzId);
        Assert.notNull(appQuartz, "fail resumeJob");
        String rs = jobUtil.resumeJob(appQuartz.getJobName(), appQuartz.getJobGroup());
        return new ReturnMsg("200","success resumeJob");
    }


    //删除job
    @RequestMapping(value="/deletJob", method=RequestMethod.GET)
    public ReturnMsg deletjob(@RequestParam Integer quartzId) throws Exception {
        AppQuartz appQuartz = appQuartzService.queryOne(quartzId);
        Assert.notNull(appQuartz, "The Job is not exist");
        String ret=jobUtil.deleteJob(appQuartz);
        if("success".equals(ret)) {
            appQuartzService.deleteById(quartzId);
        }
        return new ReturnMsg("200","success deleteJob");
    }

    //修改
    @RequestMapping(value="/updateJob", method=RequestMethod.POST)
    public ReturnMsg  modifyJob(@RequestBody AppQuartz appQuartz) throws Exception {
        String ret= jobUtil.modifyJob(appQuartz);
        if("success".equals(ret)) {
            appQuartzService.updateAppQuartzSer(appQuartz);
            return new ReturnMsg("200","success updateJob", ret);
        }
        return new ReturnMsg("404","fail updateJob", ret);
    }

    //暂停所有
    @RequestMapping(value="/pauseAll",method=RequestMethod.GET)
    public ReturnMsg pauseAllJob() throws Exception {
        jobUtil.pauseAllJob();
        return new ReturnMsg("200","success pauseAll");
    }

    //恢复所有
    @RequestMapping(value="/repauseAll",method=RequestMethod.GET)
    public ReturnMsg repauseAllJob() throws Exception {
        jobUtil.resumeAllJob();
        return new ReturnMsg("200","success repauseAll");
    }


    // https://mp.weixin.qq.com/s/lnYZ0uRcynjI9FHQiiOFig
    // 给任务添加监听器， add by cdd later
    @GetMapping("/addListener")
    public ReturnMsg addListener() throws SchedulerException {
        CustomGlobalJobListener customGlobalJobListener = new CustomGlobalJobListener();
        Matcher<JobKey> matcher = GroupMatcher.anyGroup();
        jobUtil.addJobListener(customGlobalJobListener, matcher);
        return new ReturnMsg("200","success addListener");
    }

    // 任务立即执行一次  add by cdd later
    @GetMapping("/once")
    public void once() throws SchedulerException {
        AppQuartz appQuartz = appQuartzService.queryOne(19);
        jobUtil.runJobOnce(appQuartz);
    }
}
